<template>
  <a-modal :open="open" width="640px" :title="title" centered :ok-button-props="{ disabled: !isCompleted }" ok-text="保存" @ok="onOk" @cancel="onCancel">
    <a-form class="form-style-flex" name="validate_other" :model="formState">
      <div v-if="title == '修改入库单'" class="w-full">
        <a-form-item label="入库单号" class="w-1/2">
          <a-input v-model:value="formState.rkdh" disabled placeholder="入库单号" />
        </a-form-item>
      </div>
      <a-form-item label="入库仓库" class="w-1/2" v-bind="validateInfos.ckmc">
        <SelectWarehouse v-model:value="formState.ckmc" @select="onSelectWarehouse" />
      </a-form-item>
      <a-form-item label="入库日期" class="w-1/2" v-bind="validateInfos.rksj">
        <a-date-picker v-model:value="formState.rksj" value-format="YYYY-MM-DD" class="w-full" placeholder="入库日期" />
      </a-form-item>
      <a-form-item label="供应商" class="w-full" v-bind="validateInfos.gysmc">
        <SelectTable v-model:value="formState.gysmc" type="供应商" @select="onSelectSupplier" />
      </a-form-item>
      <a-form-item label="发票状态" class="w-1/2">
        <a-radio-group v-model:value="formState.fpzt">
          <a-radio :value="0">普通</a-radio>
          <a-radio :value="1">在途</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="发票号码" class="w-1/2" v-bind="validateInfos.fph">
        <a-input v-model:value="formState.fph" :disabled="formState.fpzt" placeholder="发票号码" />
      </a-form-item>
      <a-form-item label="发票日期" class="w-1/2" v-bind="validateInfos.fprq">
        <a-date-picker v-model:value="formState.fprq" :disabled="formState.fpzt" value-format="YYYY-MM-DD" class="w-full" placeholder="发票日期" />
      </a-form-item>
      <a-form-item label="制单人:" class="w-1/2">
        {{ empName }}
      </a-form-item>
      <a-form-item label="备注" class="w-full mb-3">
        <a-textarea v-model:value="formState.remarks" :rows="2" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SelectWarehouse from '@/components/SelectWarehouse'
// import SelectItem from '@/components/SelectItem'

import useUserStore from '@/store/user'

const { empId, empName } = useUserStore()
const router = useRouter()

const emit = defineEmits(['update:open', 'update:value', 'success'])

const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  },
  title: {
    type: String,
    default: ''
  }
})

const formState = ref({
  fpzt: 0,
  rkdh: '',
  gysmc: '',
  rksj: '',
  ckmc: '',
  fph: '',
  fprq: '',
  remarks: ''
})
const rulesState = ref({
  ckmc: [{ required: true, message: '请选择入库仓库' }],
  gysmc: [{ required: true, message: '请选择供应商' }],
  rksj: [{ required: true, message: '请选择入库时间' }],
  fph: [{ required: formState.value.fpzt ? true : false, message: '请选择发票号码' }],
  fprq: [{ required: formState.value.fpzt ? true : false, message: '请选择发票日期' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)
watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.rowData?.rkdh) {
        formState.value = props.rowData
        formState.value.fpzt = +formState.value.fpzt
      } else {
        formState.value = { fpzt: 0 }
      }
    }
  }
)

const onOk = async () => {
  await validate()
  const formData = {
    czr: empId,
    rkdh: formState.value.rkdh || '',
    ...formState.value
  }
  const res = await post('/drugStorage/psiform/doSaveWarehousingHead', formData)
  if (res.code === 0) {
    message.success(res.msg)
    if (!props.rowData?.rkdh) {
      router.push({
        path: '/drug/warehouse/into-store-detail',
        query: {
          rkdh: res.data.rkdh
        }
      })
    }

    emit('update:open', false)
    emit('success')
  } else {
    message.error(res.msg)
  }
}

const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onSelectSupplier = (supplier) => {
  formState.value.gysbh = supplier?.gysbh
  formState.value.gysmc = supplier?.gysmc
}
const onSelectWarehouse = (_, supplier) => {
  formState.value.rkckbh = supplier?.ckid
  formState.value.ckmc = supplier?.ckmc
}
</script>
<style lang="less" scoped></style>
